package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JOptionPane;
import model.Lab_Student;
import model.Skip;
import model.Student;
import view.AdminDlg;
import view.AdminWnd;
import view.GroupDlg;
import view.GtsDlg;
import view.InterimDlg;
import view.LabDlg;
import view.Lab_StudentDlgU;
import view.LessonDlgU;
import view.SkipDlg;
import view.StudentDlg;
import view.SubjectDlg;
import view.TeacherDlg;
import view.TeacherWnd;

/**
 *
 * @author cska
 *
 */

public class updateAdapter implements ActionListener {

    protected AdminWnd adminWnd;
    protected AdminCtrl adminCtrl;
    protected TeacherWnd teacherWnd;
    protected TeacherCtrl teacherCtrl;
    protected int updateMode;

    public static final int UPDATE_GROUP    =   1;
    public static final int UPDATE_STUDENT  =   2;
    public static final int UPDATE_TEACHER  =   3;
    public static final int UPDATE_SUBJECT  =   4;
    public static final int UPDATE_GTS      =   5;
    public static final int UPDATE_ADMIN    =   6;
    public static final int UPDATE_LESSON   =   7;
    public static final int UPDATE_SKIP     =   8;
    public static final int UPDATE_LAB      =   9;
    public static final int UPDATE_LS       =  10;
    public static final int UPDATE_INTERIM  =  11;

    public updateAdapter(AdminWnd adminWnd, AdminCtrl adminCtrl, int updateMode) {
        this.adminWnd = adminWnd;
        this.adminCtrl = adminCtrl;
        this.updateMode = updateMode;
    }

    public updateAdapter(TeacherWnd teacherWnd, TeacherCtrl teacherCtrl, int updateMode) {
        this.teacherWnd = teacherWnd;
        this.teacherCtrl = teacherCtrl;
        this.updateMode = updateMode;
    }

    public void actionPerformed(ActionEvent e) {
        switch (updateMode) {
            case UPDATE_GROUP:
                updateGroup();
                break;
            case UPDATE_STUDENT:
                updateStudent();
                break;
            case UPDATE_TEACHER:
                updateTeacher();
                break;
            case UPDATE_SUBJECT:
                updateSubject();
                break;
            case UPDATE_GTS:
                updateGts();
                break;
            case UPDATE_ADMIN:
                updateAdmin();
                break;
            case UPDATE_LESSON:
                updateLesson();
                break;
            case UPDATE_SKIP:
                updateSkip();
                break;
            case UPDATE_LAB:
                updateLab();
                break;
            case UPDATE_LS:
                updateLs();
                break;
            case UPDATE_INTERIM:
                updateInterim();
                break;
        }
    }

    private void updateGroup() {
        if (adminWnd.getGroup() != -1) {
            GroupDlg gd = new GroupDlg(adminWnd, true, "Изменить группу", adminCtrl.getGroup(adminWnd.getGroup()));
            if (gd.fl) {
                adminCtrl.updateGroup(gd.getGroup());
                adminWnd.reloadGroup();
            }
            gd.dispose();
        } else
            showMessage("Не выбрана группа");
    }

    private void updateStudent() {
        if (adminWnd.getGroup() != -1) {
            if (adminWnd.getStudent() != -1) {
                StudentDlg sd = new StudentDlg(adminWnd, true, "Изменить студента", adminCtrl.getStudent(adminWnd.getGroup(), adminWnd.getStudent()), null);
                if (sd.fl) {
                    adminCtrl.updateStudent(sd.getStudent());
                    adminWnd.reloadStudent();
                }
            } else
                showMessage("Не выбран студент");
        } else
            showMessage("Не выбрана группа");
    }

    private void updateTeacher() {
        if (adminWnd.getTeacher() != -1) {
            TeacherDlg td = new TeacherDlg(adminWnd, true, "Изменить преподавателя", adminCtrl.getTeacher(adminWnd.getTeacher()));
            if (td.fl) {
                adminCtrl.updateTeacher(td.getTeacher());
                adminWnd.reloadTeacher();
            }
            td.dispose();
        } else
            showMessage("Не выбран преподаватель");
    }

    private void updateSubject() {
        if (adminWnd.getSubject() != -1) {
            SubjectDlg sd = new SubjectDlg(adminWnd, true, "Изменить предмет", adminCtrl.getSubject(adminWnd.getSubject()));
            if (sd.fl) {
                adminCtrl.updateSubject(sd.getSubject());
                adminWnd.reloadSubject();
            }
        sd.dispose();
        } else
            showMessage("Не выбран предмет");
    }

    private void updateGts() {
        if (adminWnd.getGts() != -1) {
            javax.swing.DefaultComboBoxModel group = adminCtrl.groupCombo();
            javax.swing.DefaultComboBoxModel teacher = adminCtrl.teacherCombo();
            javax.swing.DefaultComboBoxModel subject = adminCtrl.subjectCombo();
            int gIndex = adminCtrl.getGroupIndex(adminWnd.getGts());
            int tIndex = adminCtrl.getTeacherIndex(adminWnd.getGts());
            int sIndex = adminCtrl.getSubjectIndex(adminWnd.getGts());
            if (group.getSize()*teacher.getSize()*subject.getSize() != 0) {
                GtsDlg gd = new GtsDlg(adminWnd, true, "Изменить назначение", group, teacher, subject, gIndex, tIndex, sIndex);
                if (gd.fl) {
                    adminCtrl.updateGts(adminWnd.getGts(),gd.getGroup(),gd.getTeacher(),gd.getSubject());
                    adminWnd.reloadGts();
                }
            }
        } else
            showMessage("Не выбрано назначение");
    }

    private void updateAdmin() {
        if (adminWnd.getAdmin() != -1) {
            AdminDlg ad = new AdminDlg(adminWnd, true, "Изменить администратора", null);
            if (ad.fl) {
                adminCtrl.updateAdmin(ad.getAdmin());
                adminWnd.reloadAdmin();
            }
            ad.dispose();
        } else
            showMessage("Не выбран администратор");
    }

    private void updateLesson() {
        if (teacherWnd.getLesson() != -1) {
            LessonDlgU ld = new LessonDlgU(teacherWnd, true, "Изменить занятие", teacherCtrl.getLesson(teacherWnd.getSubject(), teacherWnd.getGroup(), teacherWnd.getLesson(), teacherWnd.getBegin(), teacherWnd.getEnd()));
            if (ld.fl) {
                teacherCtrl.updateLesson(ld.getLesson());
                teacherWnd.reloadLesson();
            }
            ld.dispose();
        } else
            showMessage("Не выбрано занятие");
    }

    private void updateSkip() {
        if (teacherWnd.getSkip() != -1) {
            List<Student> students = teacherCtrl.getStudents(teacherWnd.getSubject(), teacherWnd.getGroup(), teacherWnd.getLesson(), teacherWnd.getSkip(), teacherWnd.getBegin(), teacherWnd.getEnd());
            Skip skip = teacherCtrl.getSkip(teacherWnd.getSubject(), teacherWnd.getGroup(), teacherWnd.getLesson(), teacherWnd.getSkip(), teacherWnd.getBegin(), teacherWnd.getEnd());
            SkipDlg sd = new SkipDlg(teacherWnd, true, "Изменить отсутвующего", skip, null, students);
            if (sd.fl) {
                teacherCtrl.updateSkip(sd.getSkip());
                teacherWnd.reloadSkip();
            }
            sd.dispose();
        } else
            showMessage("Не выбран отсутсвующий");
    }

    private void updateLab() {
        if (teacherWnd.getLab() != -1) {
            LabDlg ld = new LabDlg(adminWnd, true, "Изменить задание", teacherCtrl.getLab(teacherWnd.getSubject_l(), teacherWnd.getBegin_l(), teacherWnd.getEnd_l(), teacherWnd.getLab()), null);
            if (ld.fl) {
                teacherCtrl.updateLab(ld.getLab());
                teacherWnd.reloadLab();
            }
            ld.dispose();
        } else
            showMessage("Не выбрано задание");
    }

    private void updateLs() {
        if (teacherWnd.getLs() != -1) {
            Lab_Student ls = teacherCtrl.getLs(teacherCtrl.getLab(teacherWnd.getSubject_l(), teacherWnd.getBegin_l(), teacherWnd.getEnd_l(), teacherWnd.getLab()), teacherCtrl.getGroup(teacherWnd.getSubject_l(), teacherWnd.getGroup_ls()), teacherWnd.getLs());
            Lab_StudentDlgU lsd = new Lab_StudentDlgU(teacherWnd, true, "Изменить запись", ls);
            if (lsd.fl) {
                teacherCtrl.updateLs(lsd.getLs());
                teacherWnd.reloadLs();
            }
            lsd.dispose();
        } else
            showMessage("Не выбрана запись");
    }

    private void updateInterim() {
        if (teacherWnd.getInterim() != -1) {
            InterimDlg id = new InterimDlg(teacherWnd, true, "Изменить промежуточную сдачу", teacherCtrl.getInterim(teacherWnd.getSubject_l(), teacherWnd.getLab(), teacherWnd.getGroup_ls(), teacherWnd.getLs(), teacherWnd.getInterim(), teacherWnd.getBegin_l(), teacherWnd.getEnd_l()), null);
            if (id.fl) {
                teacherCtrl.updateInterim(id.getInterim());
                teacherWnd.reloadInterim();
            }
            id.dispose();
        } else
            showMessage("Не выбрана промежуточная сдача");
    }

    private void showMessage(String content) {
        JOptionPane.showMessageDialog(adminWnd, content, "Ошибка", JOptionPane.ERROR_MESSAGE);
    }

}
